<?php
// +----------------------------------------------------------------------
// | 07FLYCRM [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\ten\controller;

use think\db;

/**
 * 用户控制器
 */
class TenModule extends TenBase
{

    /**
     * 列表
     */
    public function show()
    {
        return $this->fetch('show');
    }

    /**
     * 列表json数据
     */
    public function show_json()
    {
        $where = [];
        if (!empty($this->param['keywords'])) {
            $where['name|remark'] = ['like', '%' . $this->param['keywords'] . '%'];
        }
        $list = $this->logicTenModule->getTenModuleList($where);
        return $list;
    }


    /**
     * 添加
     */
    public function add()
    {
        IS_POST && $this->jump($this->logicTenModule->tenModuleAdd($this->param));
        return $this->fetch('add');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        IS_POST && $this->jump($this->logicTenModule->tenModuleEdit($this->param));
        $info = $this->logicTenModule->getTenModuleInfo(['id' => $this->param['id']]);
        $this->assign('info', $info);
        return $this->fetch('edit');
    }

    /**
     * 删除
     */
    public function del()
    {
        $where = empty($this->param['id']) ? ['id' => 0] : ['id' => $this->param['id']];
        $this->jump($this->logicTenModule->tenModuleDel($where));
    }

    /**
     * 菜单授权
     */
    public function menuAuth()
    {

        IS_POST && $this->jump($this->logicTenModule->setTenModuleMenus($this->param));

        //所有菜单的节点
        $where = $this->logicTenMenu->getWhere($this->param);
        $menuListTree = $this->logicTenMenu->getTenMenuListTree($where, 'id,name,pid', 'sort asc');

        //当前模块信息,已经配置的菜单
        $moduleInfo = $this->logicTenModule->getTenModuleInfo(['id' => $this->param['id']]);

        // 菜单转换为多选视图，支持无限级
        $menu_view = $this->logicTenMenu->menuTreeCheckboxView($menuListTree, 'children', $moduleInfo['menu_list']);

        $this->assign('list', $menu_view);
        $this->assign('id', $this->param['id']);
        return $this->fetch('menu_auth');
    }

    /**
     * 启用
     */
    public function set_visible()
    {
        $this->jump($this->logicTenBase->setField('TenModule', $this->param));
    }

    /**
     * 排序
     */
    public function set_sort()
    {
        $this->jump($this->logicTenBase->setSort('TenModule', $this->param));
    }

    /**
     * 字段修改
     */
    public function set_field()
    {
        $this->jump($this->logicTenBase->setField('TenModule', $this->param));
    }

    /**
     * 模块同步
     */
    public function synctable()
    {
        return $this->jump($this->logicTenModule->tenModuleSyncTable($this->param));
    }

    /**
     * 模块同步
     */
    public function syncmenu()
    {
        return $this->jump($this->logicTenModule->tenModuleSyncMenu($this->param));
    }

    /**
     * 模块上传
     */
    public function upload()
    {
        if (!empty($this->param['ajaxmodel'])) {
            switch ($this->param['ajaxmodel']) {
                case 'get':
                    return $this->logicUpload->getUploadFile($this->param);
                    break;
                case 'del':
                    return $this->jump($this->logicUpload->delUploadFile($this->param));
                    break;
                case 'upload':
                    return $this->jump($this->logicUpload->uploadFile($this->param));
                    break;
                case 'install':
                    return $this->jump($this->logicTenModule->tenModuleUpload($this->param));
                    break;
            }
        }
        $uploadfilepath = 'tenmodule_' . SYS_USER_ID;//上传文件目录
        $uploadtarget = url('TenModule/upload', array('ajaxmodel' => 'upload', 'uploadfilepath' => $uploadfilepath));
        $this->assign('uploadfilepath', $uploadfilepath);
        $this->assign('ajaxtarget', url());
        $this->assign('uploadtarget', $uploadtarget);
        return $this->fetch('upload');//上传接口模板
    }
}
